{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  章建强\n",
    "+ 学号0163570"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是否爱就得忍耐 不问该不该\n",
      "都怪我没能耐 转身走开\n",
      "难道牺牲才精彩 伤痛才实在\n",
      "要为你流下泪来 才证明是爱\n",
      "\n",
      "如果这都不算爱\n",
      "我有什么好悲哀\n",
      "谢谢你的慷慨\n",
      "是我自己活该\n",
      "如果这都不算爱\n",
      "我有什么好悲哀\n",
      "你只要被期待\n",
      "不要真正去爱 OH NO\n",
      "\n",
      "是否爱就得忍耐 不问该不该\n",
      "都怪我没能耐 转身走开\n",
      "难道牺牲才精彩 伤痛才实在\n",
      "要为你流下泪来 才证明是爱\n",
      "\n",
      "你的感情\n",
      "太易割爱\n",
      "把未来转眼就删改\n",
      "我的心却为你空白了一块\n",
      "\n",
      "如果这都不算爱\n",
      "我有什么好悲哀\n",
      "谢谢你的慷慨\n",
      "是我自己活该\n",
      "如果这都不算爱\n",
      "我有什么好悲哀\n",
      "你只要被期待\n",
      "不要真正去爱 OH NO\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def la():\n",
    "    print(\"是否爱就得忍耐 不问该不该\\n都怪我没能耐 转身走开\\n难道牺牲才精彩 伤痛才实在\\n要为你流下泪来 才证明是爱\\n\")\n",
    "\n",
    "def lb():\n",
    "    print(\"如果这都不算爱\\n我有什么好悲哀\\n谢谢你的慷慨\\n是我自己活该\\n如果这都不算爱\\n我有什么好悲哀\\n你只要被期待\\n不要真正去爱 OH NO\\n\")\n",
    "def lc():\n",
    "    print(\"你的感情\\n太易割爱\\n把未来转眼就删改\\n我的心却为你空白了一块\\n\")\n",
    "la()\n",
    "lb()\n",
    "la()\n",
    "lc()\n",
    "lb()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该样本均值为：18.0\n",
      "该样本有n=13个样本观测值\n",
      "样本方差为:15.166666666666666\n",
      "\n",
      "该样本均值为：106.0\n",
      "该样本有n=13个样本观测值\n",
      "样本方差为:15.166666666666666\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def numbers(*args):\n",
    "    a=0\n",
    "    n=0\n",
    "    sum=0\n",
    "    for value in args:\n",
    "        sum+=value\n",
    "        n+=1\n",
    "    medium=sum/n    \n",
    "    print('该样本均值为：%s'%(medium))\n",
    "    print('该样本有n=%s个样本观测值'%n)\n",
    "    for value in args:\n",
    "        a+=(value-medium)**2 \n",
    "    varible=a/(n-1)    \n",
    "    print('样本方差为:%s\\n'%(varible))\n",
    "    \n",
    "numbers(12,13,14,15,16,17,18,19,20,21,22,23,24)    \n",
    "numbers(100,101,102,103,104,105,106,107,108,109,110,111,112)       "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该样本均值为：18.0\n",
      "该样本有n=13个样本观测值\n",
      "该样本均值为：18.0\n",
      "该样本有n=13个样本观测值\n",
      "样本方差为:15.166666666666666\n",
      "该样本均值为：18.0\n",
      "该样本有n=13个样本观测值\n",
      "样本方差为:15.166666666666666\n",
      "样本标准差为:3.8944404818493075\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def numbers(*args,var=None,stdvar=None):\n",
    "    a=0\n",
    "    n=0\n",
    "    sum=0\n",
    "    for value in args:\n",
    "        sum+=value\n",
    "        n+=1\n",
    "    medium=sum/n    \n",
    "    print('该样本均值为：%s'%(medium))\n",
    "    print('该样本有n=%s个样本观测值'%n)\n",
    "    for value in args:\n",
    "        a+=(value-medium)**2 \n",
    "    varible=a/(n-1)\n",
    "    if var:\n",
    "        print('样本方差为:%s'%(varible))\n",
    "        \n",
    "    if stdvar:\n",
    "        print('样本标准差为:%s'%math.sqrt(varible))\n",
    "        \n",
    "numbers(12,13,14,15,16,17,18,19,20,21,22,23,24)    \n",
    "numbers(12,13,14,15,16,17,18,19,20,21,22,23,24,var=2)    \n",
    "numbers(12,13,14,15,16,17,18,19,20,21,22,23,24,var=1,stdvar=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "样本标准误差为:3.8944404818493075\n",
      "最大值为： None\n",
      "最小值为： None\n",
      "样本标准误差为:3.8944404818493075\n",
      "最大值为： 24\n",
      "最小值为： None\n",
      "样本标准误差为:3.8944404818493075\n",
      "最大值为： 24\n",
      "最小值为： 1\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def numbers(*args,stdvar=None,max=None,min=None):\n",
    "    n=0\n",
    "    b=0\n",
    "    sum=0\n",
    "    if stdvar:\n",
    "        for value in args:\n",
    "            sum+=value\n",
    "            n=n+1\n",
    "        med=sum/n    \n",
    "        for value in args:\n",
    "            b+=(value-med)**2\n",
    "        vari=b/(n-1)\n",
    "        std=math.sqrt(vari)\n",
    "    print('样本标准误差为:%s'%std)\n",
    "    if max:\n",
    "        for value in args:\n",
    "            if value>max:\n",
    "                max=value\n",
    "    print('最大值为：', max)\n",
    "    if min:\n",
    "        for value in args:\n",
    "            if value<min:\n",
    "                min=value\n",
    "    print('最小值为：', min)\n",
    "        \n",
    "numbers(12,13,14,15,16,17,18,19,20,21,22,23,24,stdvar=2)           \n",
    "numbers(12,13,14,15,16,17,18,19,20,21,22,23,24,stdvar=2,max=1)          \n",
    "numbers(12,13,14,15,16,17,18,19,20,21,22,23,24,stdvar=2,max=1,min=1)    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
